package common

import (
	"io"
	"log"
	"net/http"
	"strings"
)

func GetUrl(url string, token string) (string, error) {
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		log.Println(err)
	}
	req.Header.Add("Authorization", "Bearer "+token)
	//获取客户端对象，执行请求
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		log.Println(err)
		return "", err
	}
	defer resp.Body.Close()

	//获取返回值
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		log.Println(err)
		return "", err
	}
	return string(body), nil
}

func PostUrl(url string, data string) (string, error) {
	req, _ := http.NewRequest("POST", url, strings.NewReader(data))
	// 如果以json格式传输时，一定要加上这个规定数据形式的
	req.Header.Set("Content-Type", "application/json")
	//获取客户端对象，执行请求

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		log.Println(err)
		return "", err
	}
	defer resp.Body.Close()

	//获取返回值
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		log.Println(err)
		return "", err
	}
	return string(body), nil
}
